package com.sgb.kjwl.viewmodel.enterprise;

import android.app.Application;

import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.MutableLiveData;

import com.sgb.kjwl.api.APIManager;
import com.sgb.kjwl.model.entity.enterprise.NeedListItemEntity;
import com.swgk.core.base.model.entity.BaseEntity;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class AddNeedDetailModel extends AndroidViewModel {

    public MutableLiveData<NeedListItemEntity> data = new MutableLiveData<>();
    public MutableLiveData<Integer>show_loading = new MutableLiveData<>();
    public boolean gotomore = false;

    public AddNeedDetailModel(@NonNull Application application) {
        super(application);
    }

    /**
     * 删除
     */
    public void detletdata(){
        show_loading.postValue(1);

        APIManager.getInstance().getNeedAPI().delNeedMessageType(data.getValue().getId()).enqueue(new Callback<BaseEntity<Object>>() {
            @Override
            public void onResponse(Call<BaseEntity<Object>> call, Response<BaseEntity<Object>> response) {
                show_loading.postValue(2);
                if(response.body()!=null){
                    show_loading.postValue(3);
                }else{

                }

            }

            @Override
            public void onFailure(Call<BaseEntity<Object>> call, Throwable t) {
                show_loading.postValue(2);

            }
        });
    }

    /**
     * 上架下架
     */
    public void updateType(){
        show_loading.postValue(1);

        APIManager.getInstance().getNeedAPI().updateNeedMessageType(data.getValue().getId(),data.getValue().getDemandStatus().equals("1")?"-1":"1").enqueue(new Callback<BaseEntity<Object>>() {
            @Override
            public void onResponse(Call<BaseEntity<Object>> call, Response<BaseEntity<Object>> response) {
                show_loading.postValue(2);
                if(response.body()!=null){
                    NeedListItemEntity updata = data.getValue();
                    updata.setDemandStatus(data.getValue().getDemandStatus().equals("1")?"-1":"1");
                    data.postValue(updata);
                }else{

                }

            }

            @Override
            public void onFailure(Call<BaseEntity<Object>> call, Throwable t) {
                show_loading.postValue(2);

            }
        });

    }
}
